package com.ym521.freesnack.core

import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.google.android.material.snackbar.BaseTransientBottomBar
import com.google.android.material.snackbar.Snackbar
import com.ym521.freesnack.R
import com.ym521.freesnack.theme.FreeSnackTheme
import com.ym521.freesnack.utils.PixelUtil

/**
 *@author Ym
 *E-mail: 2435970206@qq.com
 *createTime:2024/3/3
 *explain:
 *
 */
internal class FreeSnackBuilder : IFreeSnack {

    // FreeSnack显示 高度
    private val ELEVATION_DEFAULT = 20f

    /**
     * 顶部显示
     */
    override fun snackBarTop(
        view: View,
        content: String,
        time: Int,
        theme: FreeSnackTheme
    ) {
        val snackBar = Snackbar.make(view, content, time)
        snackBar.view.apply {
            setBackgroundResource(
                when (theme) {
                    FreeSnackTheme.LIGHT -> R.drawable.freesnackbar_bg_white_shape
                    FreeSnackTheme.DARK -> R.drawable.freesnackbar_bg_black_shape
                }
            )
            elevation = ELEVATION_DEFAULT
            when (layoutParams) {
                is FrameLayout.LayoutParams -> {
                    val layoutParams = layoutParams as FrameLayout.LayoutParams
                    layoutParams.gravity = Gravity.TOP or Gravity.CENTER_HORIZONTAL
                    layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT
                    layoutParams.topMargin = PixelUtil.dip2px(view, 100f)
                }
                is CoordinatorLayout.LayoutParams -> {
                    val layoutParams = layoutParams as CoordinatorLayout.LayoutParams
                    layoutParams.gravity = Gravity.TOP or Gravity.CENTER_HORIZONTAL
                    layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT
                    layoutParams.topMargin = PixelUtil.dip2px(view, 100f)
                }
            }
        }
        snackBar.animationMode = BaseTransientBottomBar.ANIMATION_MODE_FADE
        snackBar.setTextColor(theme.textColor)
        snackBar.setBackgroundTint(theme.backgroundColor)
        snackBar.show()
    }

    /**
     * 中间显示
     */
    override fun snackBarCenter(
        view: View,
        content: String,
        time: Int,
        theme: FreeSnackTheme
    ) {
        val snackBar = Snackbar.make(view, content, time)
        snackBar.view.apply {
            setBackgroundResource(
                when (theme) {
                    FreeSnackTheme.LIGHT -> R.drawable.freesnackbar_bg_white_shape
                    FreeSnackTheme.DARK -> R.drawable.freesnackbar_bg_black_shape
                }
            )
            elevation = ELEVATION_DEFAULT
            when (layoutParams) {
                is FrameLayout.LayoutParams -> {
                    val layoutParams = layoutParams as FrameLayout.LayoutParams
                    layoutParams.gravity = Gravity.CENTER
                    layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT
                }
                is CoordinatorLayout.LayoutParams -> {
                    val layoutParams = layoutParams as CoordinatorLayout.LayoutParams
                    layoutParams.gravity = Gravity.CENTER
                    layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT
                }
            }

        }
        snackBar.animationMode = BaseTransientBottomBar.ANIMATION_MODE_FADE
        snackBar.setTextColor(theme.textColor)
        snackBar.setBackgroundTint(theme.backgroundColor)
        snackBar.show()
    }

    /**
     * 底部显示
     */
    override fun snackBarBottom(
        view: View,
        content: String,
        time: Int,
        theme: FreeSnackTheme
    ) {
        val snackBar = Snackbar.make(view, content, time)
        snackBar.view.apply {
            setBackgroundResource(
                when (theme) {
                    FreeSnackTheme.LIGHT -> R.drawable.freesnackbar_bg_white_shape
                    FreeSnackTheme.DARK -> R.drawable.freesnackbar_bg_black_shape
                }
            )
            elevation = ELEVATION_DEFAULT
            when (layoutParams) {
                is FrameLayout.LayoutParams -> {
                    val layoutParams = layoutParams as FrameLayout.LayoutParams
                    layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT
                    layoutParams.topMargin = (view.height * 0.3f).toInt()
                    layoutParams.gravity = Gravity.CENTER or Gravity.CENTER_HORIZONTAL
                    this.layoutParams = layoutParams
                }
                is CoordinatorLayout.LayoutParams -> {
                    val layoutParams = layoutParams as CoordinatorLayout.LayoutParams
                    layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT
                    layoutParams.topMargin = (view.height * 0.3f).toInt()
                    layoutParams.gravity = Gravity.CENTER or Gravity.CENTER_HORIZONTAL
                    this.layoutParams = layoutParams
                }
            }
        }
        snackBar.animationMode = BaseTransientBottomBar.ANIMATION_MODE_FADE
        snackBar.setTextColor(theme.textColor)
        snackBar.setBackgroundTint(theme.backgroundColor)
        snackBar.show()
    }
}